//
//  multiLineTextField.swift
//  MyNotes
//
//  Created by YES on 2020/3/20.
//  Copyright © 2020 YES. All rights reserved.
//

import SwiftUI
import UIKit

//struct
//class
struct multiLineTextField : UIViewRepresentable
//, UIView,UIScrollViewDelegate
{
    @Binding var text : String
    @State var editable: Bool = true
    @State var firstEdit: Bool = true
//  然而他每次进来都是。第一次的
    func makeCoordinator() -> multiLineTextField.Coordinator {
       let  coordinator=multiLineTextField.Coordinator(parent1: self)
        coordinator.firstEdit=firstEdit
//        return multiLineTextField.Coordinator(parent1: self)
        return coordinator
    }
    
    /// Proxy object for prevending a reference cycle between the CADDisplayLink and AnimatedImageView.
       class TargetProxy {
//           private weak var target: multiLineTextField?
           private  var target: multiLineTextField?
           init(target: multiLineTextField) {
               self.target = target
           }
           
          
           
           @objc    func clear(){
               print( " target?.text")
               print("target")
               print(target)
               target?.text = ""
//               self.target ? .text = ""
                  }
       }
    
    func makeUIView(context: UIViewRepresentableContext<multiLineTextField>) -> UITextView{
        let textview = UITextView()
        textview.font = .systemFont(ofSize: 18)
        textview.delegate = context.coordinator
        
        let toolBar = UIToolbar(frame: CGRect(x:0,y:0,width: textview.frame.size.width, height: 44))
        let doneButton = UIBarButtonItem(title: "done", style: .done, target: self, action: #selector(textview.doneBUttonTapped(button:)))
        // 使用
//        button.addTarget(self, action: Action.tapped, for: .touchUpInside)
//        没反应
//        let clearButton = UIBarButtonItem(title: "clear", style: .done,
//                                          target: self,
////                                          target:  TargetProxy(target: self),
////                                          action: #selector(clear(_:))
//
////                                          action: #selector(clear)
////                                          action: #selector(TargetProxy.clear)
////                                          action: #selector(multiLineTextField.clear(_:))
//                                          action: #selector(multiLineTextField.clear)
////                                          action: #selector(sayHello(_:))
////                                          action: #selector(clear(sender:))
//
//        )
//
     let   targetProxy   = TargetProxy(target: self)
        let clearButton = UIBarButtonItem(title: "clear", style: .done,
                                          target: targetProxy,
                                          action: #selector(targetProxy.clear)
        )
        
//        let clearButton = UIBarButtonItem(title: "clear", style: .done,
//                                          target:  TargetProxy(target: self),
//                                          action: #selector(TargetProxy.clear)
//        )
//        let clearButton = UIBarButtonItem(title: "clear", style: .done, target: self, action:#selector(clear))
//        let clearButton = UIBarButtonItem(title: "clear", style: .done, target: self, action:clear)
//        let clearButton = UIBarButtonItem(title: "clear", style: .done, target: self, action:clear())
//        clear()
//        clear()
//        clear()
//        multiLineTextField.updateUIView(_:context:)
        if editable{
//            toolBar.items = [doneButton]
//            toolBar.setItems([doneButton], animated: true)
            toolBar.setItems([doneButton,clearButton], animated: true)
            textview.inputAccessoryView = toolBar
            textview.textColor = .gray
        }
        else{
            textview.textColor = UIColor(named: "TextColor")
        }
        //textView透明背景
        //textview.backgroundColor = .clear
        
        textview.isUserInteractionEnabled = true
        textview.isEditable = editable
        textview.backgroundColor = UIColor(named: "RowAnyColor")
        
        return textview
    }
//    func clear(){
//           uiView.text = ""
//       }
//    func clear(sender: UIBarButtonItem){
//        self.text = ""
//    }
//    @objc func clear(){
//        self.text = ""
//    }
    
//    @objc func clear(sender: UIBarButtonItem){
//        self.text = ""
//    }
    
    func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<multiLineTextField>) {
        uiView.text = text
    }
    
    class Coordinator : NSObject,UITextViewDelegate{
        var firstEdit:Bool = true
        var parent : multiLineTextField
        init(parent1 : multiLineTextField) {
            parent = parent1
        }
        
        func textViewDidBeginEditing(_ textView: UITextView) {
//            if firstEdit{
//                textView.text = ""
//            }else{
//                firstEdit=false
//            }
//

                   //textView.text = ""
                   textView.textColor = UIColor(named: "TextColor")
               }
        
        func textViewDidChange(_ textView: UITextView) {
            self.parent.text = textView.text
        }
    }
}


extension UITextView{
    @objc func doneBUttonTapped(button:UIBarButtonItem) -> Void{
        self.resignFirstResponder()
    }
    @objc func clearButtonTapped(button:UIBarButtonItem) -> Void{
        self.text=""
    }
}


// 定义
//struct Action {
//  static let tapped: Selector = #selector(SomeClass.tap(_:))
//}
//
class UIKitController: UIViewController {
    @objc func openContentView() {
          
          let hostVC = UIHostingController(rootView: ContentView())
          present(hostVC, animated: true, completion: nil)
      }
}

//@objc func buttonClicked(sender: UIBarButtonItem) {
//
//}
